package cn.edu.sjtu.dfa;

public class Binop extends Instruction {
	private Register v1 = null;
	private DataContainer v2;
	private Register ret;
	
	private String op;	// TODO maybe def "plus", "minus"... classes
	
	public Binop(Register v1, DataContainer v2, Register ret, String op) {
		this.v1 = v1;
		this.v2 = v2;
		this.ret = ret;
		this.op = op;
	}
	
	public Register getRegister1() {
		return v1;
	}
	
	public DataContainer getRegister2() {
		return v2;
	}
	
	public Register getReturnRegister() {
		return ret;
	}
	
	public String getOP() {
		return op;
	}

	@Override
	public String toString() {
		if(v2.getClass().getName().equals("cn.edu.sjtu.dfa.Register")){
			if(v1 != null)
			return "Binop: v" + ret.getRegister() + " = " + "v" + v1.getRegister() + " " + op + " v" + ((Register) v2).getRegister();
			else
			return "Binop: v" + ret.getRegister() + " = " + op + " v" + ((Register) v2).getRegister();
		}
		else{
			if(v1 != null)
				return "Binop: v" + ret.getRegister() + " = " + "v" + v1.getRegister() + " " + op + " " + v2.getType() + " " + v2.getValue().toString();
				else
				return "Binop: v" + ret.getRegister() + " = " + op + " " + v2.getType() + " " + v2.getValue().toString();
		}
	}
	
	final public static int PLUS = 1, MINUS = 2, MUL = 3, DIV = 4, REM = 5, AND = 6, OR = 7, XOR = 8, SHL = 9, SHR = 10, USHR = 11, ERROR = 9999;
	
	public static int getOp(String opString){
		if(opString.equals("ADD"))
			return PLUS;
		else if(opString.equals("SUB"))
			return MINUS;
		else if(opString.equals("MUL"))
			return MUL;
		else if(opString.equals("DIV"))
			return DIV;
		else if(opString.equals("REM"))
			return REM;
		else if(opString.equals("AND"))
			return AND;
		else if(opString.equals("OR"))
			return OR;
		else if(opString.equals("XOR"))
			return XOR;
		else if(opString.equals("SHL"))
			return SHL;
		else if(opString.equals("SHR"))
			return SHR;
		else if(opString.equals("USHR"))
			return USHR;
		return ERROR;
	}
	
	
}
